from django.test import TestCase

# Create your tests here.


if __name__ == '__main__':
    # 外部启动django的方法
    import sys, os
    from django.core.wsgi import get_wsgi_application

    base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(base_dir)
    os.environ['DJANGO_SETTINGS_MODULE'] = 'drf_movie_rimi.settings'
    application = get_wsgi_application()
    from datetime import datetime
    from rest_framework import serializers


    # 新建一个类
    class Comment(object):
        def __init__(self, email, content, created=None):
            self.email = email
            self.content = content
            self.test = '5'
            self.created = created or datetime.now()


    # 实例化一个对象 给予邮箱和内容
    comment = Comment(email='leila@example.com', content='foo bar')


    # 定义序列化的数据要求
    class CommentSerializer(serializers.Serializer):
        email = serializers.EmailField()
        content = serializers.CharField(max_length=200)
        created = serializers.DateTimeField()




    # 序列化数据
    # serializer = CommentSerializer(comment)
    # print(serializer.data)

    # 实例化一个对象 给予邮箱和内容
    comment_error = Comment(email='leie@mple.com', content='foo bar')
    comment_data = CommentSerializer(comment_error)
    serializer_error = CommentSerializer(data=comment_data.data)
    print(serializer_error.is_valid())
    print(serializer_error.errors)
    print(serializer_error.validated_data)


    from rest_framework.renderers import JSONRenderer



    json = JSONRenderer().render(comment_data.data)

    print(json)

    from django.utils.six import BytesIO
    from rest_framework.parsers import JSONParser

    stream = BytesIO(json)
    data = JSONParser().parse(stream)

    print(data)

    pass

